<html>
<head>
<meta charset="utf-8" />
<title>Huawei HG225d Configuration Encoder/Decoder</title>
</head>
<body>

<script type="text/javascript">
function output(str) {
    var outputTextArea = document.getElementById("outputTextArea");
    outputTextArea.value = str;
}

function outputByteArray(bytes) {
    var bytesTextArea = document.getElementById("bytesTextArea");
    bytesTextArea.value = bytes.join(", ");
}

/**
 * if x <= 63 then y = x * 2 = (2,4,6,...,126)
 * if x > 63 then y * 2 - 127 = (1,3,5,...)
 */
function encode(str) {
    if (!str) {
        alert("Please input text!");
        return;
    }

    var bytes = [];
    for (var i = 0; i < str.length; i++) {
        var ch = str.charCodeAt(i);
        var code = ch * 2;

        // ch > 63
        if (code > 127) {
            code -= 127;
        }

        bytes.push(code);
    }

    outputByteArray(bytes);
    var encoded = String.fromCharCode.apply(String.fromCharCode, bytes);
    output(encoded);
}

function decode(str) {
    if (!str) {
        alert("Please input text!");
        return;
    }

    var bytes = [];
    for (var i = 0; i < str.length; i++) {
        var ch = str.charCodeAt(i);
        var code = ch;

        if (ch % 2 === 0) {
            // even, means code <= 63
            code = ch / 2;
        } else {
            // odd, means code > 63
            code = (ch + 127) / 2;
        }

        bytes.push(code);
    }

    outputByteArray(bytes);
    var decoded = String.fromCharCode.apply(String.fromCharCode, bytes);
    output(decoded);
}
</script>

<h1>Huawei HG225d Configuration Encoder/Decoder</h1>
<div>
  <p>Source</p>
  <p><textarea id="inputTextArea" wrap="soft" style="height: 200px; width: 750px"></textarea></p>
  <p>
    <button onclick="encode(document.getElementById('inputTextArea').value);">Encode</button>
    <button onclick="decode(document.getElementById('inputTextArea').value);">Decode</button>
  </p>
  <p>Result</p>
  <p><textarea id="outputTextArea" wrap="soft" style="height: 200px; width: 750px"></textarea></p>
  <p>Bytes</p>
  <p><textarea id="bytesTextArea" wrap="soft" style="height: 200px; width: 750px"></textarea></p>
</div>
</body>
</html>